﻿using CaltechSoft.Service.DBCommand;
using CaltechSoft.Service.EntityFW.Context;
using CaltechSoft.Service.EntityFW.Entity;
using CaltechSoft.Service.Infrastructure.Utility;
using CaltechSoft.Service.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaltechSoft.Service.CommandService
{
    public class EquipmentCommandService : BaseRepository<Equipment>, IEquipmentCommandService
    {
        public int CreateEquipment(CreateEquipmentCommand command)
        {
            Guard.ArgumentIsNotNull(command, "command");

            Guard.Against<ArgumentException>(string.IsNullOrEmpty(command.ModelNumber), "Model number can not be empty");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(command.Manufacturer), "Equipment manufacturer can not be empty");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(command.ModelNumber), "Model number can not be empty");

            using (var context = new CaltechContext())
            {
                var existingItem = context.Set<Equipment>().FirstOrDefault(n => n.Manufacturer == command.Manufacturer &&
                                                                                     n.ModelNumber == command.ModelNumber
                                                                                    );
                if (existingItem != null)
                {
                    return existingItem.Id;
                }
                Equipment equip = new Equipment
                {
                    Manufacturer = command.Manufacturer,
                    AssetNumber = command.AssetNumber,
                    Description = command.Description,
                    ModelNumber = command.ModelNumber,
                    PONumber = command.PONumber,
                    SerialNumber = command.SerialNumber,
                    Procedure = command.Procedure,
                    CreatedDate = command.CreatedDate
                };
                context.Set<Equipment>().Add(equip);
                context.SaveChanges();
                return equip.Id;
            }
        }


        public override async Task<int> Add(Equipment entity)
        {
            Guard.ArgumentIsNotNull(entity, "command");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(entity.Manufacturer), "Equipment manufacturer can not be empty");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(entity.ModelNumber), "Model Number can not be empty");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(entity.Name), "Description can not be empty");

            using (var context = Context)
            {
                var existingItem = context.Set<Equipment>().FirstOrDefault(n => n.Manufacturer == entity.Manufacturer.Trim() && n.ModelNumber == entity.ModelNumber.Trim());
                Guard.Against<Exception>(existingItem != null, "Duplicated Manufacturer + Model # is not allowed!");

                context.Equipments.Add(entity);
                return await context.SaveChangesAsync();
            }
        }

        public override async Task<int> Update(Equipment entity)
        {
            Guard.ArgumentIsNotNull(entity, "command");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(entity.Manufacturer), "Equipment manufacturer can not be empty");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(entity.ModelNumber), "Model Number can not be empty");
            Guard.Against<ArgumentException>(string.IsNullOrEmpty(entity.Name), "Description can not be empty"); ;

            using (var context = Context)
            {
                var existingItem = context.Set<Equipment>().FirstOrDefault(n => n.Manufacturer == entity.Manufacturer.Trim() && n.ModelNumber == entity.ModelNumber.Trim() && n.Id != entity.Id);
                Guard.Against<Exception>(existingItem != null, "Duplicated Manufacturer + Model # is not allowed!");

                return await base.Update(entity);
            }
        }

        public override async Task<int> Remove(int id)
        {
            using (var context = Context)
            {
                Guard.Against<Exception>(context.Set<QuoteItem>().Any(n => n.EquipmentId == id), "This equipment have been already used in quote!");
                Guard.Against<Exception>(context.Set<PoItem>().Any(n => n.EquipmentId == id), "This equipment have been already used in PO!");
                Guard.Against<Exception>(context.Set<WorkOrderDetails>().Any(n => n.EquipmentId == id), "This equipment have been already used in WO!");
                return await base.Remove(id);
            }
        }
    }
}
